package com.jtjrenren.android.taxi.passenger.utils;

import com.library.utils.StringUtils;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by Administrator on 2015/12/21.
 */
public class HttpParseUtils {
    public static final String FLAG_SUCCESS = "1111111";


    /**
     * 解析通过验证码校验手机号
     *
     * @param jsonStr
     * @return
     */
    public static String parseAuthPhoneByCode(String jsonStr) {
        String or = null;
        if (!(jsonStr == null || "".equals(jsonStr))) {
            try {
                JSONObject obj = new JSONObject(jsonStr);
                if (obj.has("Result")) {
                    String str = obj.getString("Result");
                    String result = parseResultCode(str);
                    if (result != null) {
                        or = result;
                    } else {
                        if ("SM0009".equals(result))
                            or = FLAG_SUCCESS;
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return or;
    }

    /**
     * 解析用户登录返回的值 格式为:
     * {"Result":"US0009"}
     * {"Result":"8f53a1ae-b3d0-441e-bc45-12bdf78f7632,13554274325,A,token123123"}
     *
     * @param jsonStr
     * @return
     */
    public static String[] parseUserCommonResult(String jsonStr) {
        if (StringUtils.isEmpty(jsonStr)) {
            return null;
        }
        try {
            String[] results;
            JSONObject obj = new JSONObject(jsonStr);
            if (obj.has("Result")) {
                String str = obj.getString("Result");
                String result = parseResultCode(str);
                if (result != null) {
                    results = new String[1];
                    results[0] = result;
                    return results;
                } else {
                    return str.split(",");
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }


    public static String parseResultCode(String str) {
        String result = null;
        if (str.indexOf("US0002") > -1)
            result = "密码未加密";
        else if (str.indexOf("US0003") > -1)
            result = "手机号已被注册";
        else if (str.indexOf("US0005") > -1)
            result = "用户信息更新失败";
        else if (str.indexOf("US0007") > -1)
            result = "手机未注册";
        else if (str.indexOf("US0008") > -1)
            result = "密码错误";
        else if (str.indexOf("US0009") > -1)
            result = "账号被锁定不能登录";
        else if (str.indexOf("US0012") > -1)
            result = "年龄格式不符合要求";
        else if (str.indexOf("SM0004") > -1)
            result = "验证码为空";
        else if (str.indexOf("SM0005") > -1)
            result = "验证码已超时";
        else if (str.indexOf("SM0006") > -1)
            result = "输入的验证码错误";
        else if (str.indexOf("SM0007") > -1)
            result = "验证码已经验证过，重复验证";
        else if (str.indexOf("SM0008") > -1)
            result = "未发送验证码";
        else if (str.indexOf("SM0010") > -1)
            result = "类型格式不符合要求";

        return result;
    }
}
